package com.qding.park.manager.service.queue.facade.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.qding.park.common.core.constant.MongodbConstant;
import com.qding.park.common.core.exceptions.BaseException;
import com.qding.park.common.core.exceptions.ExceptionMessage;
import com.qding.park.common.core.exceptions.IExceptionHandle;
import com.qding.park.common.core.exceptions.business.manager.service.ServiceCoreException;
import com.qding.park.common.utils.DateUtils;
import com.qding.park.common.utils.ListUtil;
import com.qding.park.common.utils.OrderNumUtil;
import com.qding.park.manager.cache.CarCacheInter;
import com.qding.park.manager.cache.ParkCacheInter;
import com.qding.park.manager.cache.UserCacheInter;
import com.qding.park.manager.facade.queue.service.ReportFacade;
import com.qding.park.manager.facade.queue.service.entity.ExceptionParkOutSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkEventSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkInSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkOutRenewalSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.ParkOutSyncDTO;
import com.qding.park.manager.facade.queue.service.entity.RenewalSyncDTO;
import com.qding.park.manager.facade.queue.service.enums.ArkCarTypeEnum;
import com.qding.park.manager.facade.queue.service.enums.ArkEventTypeEnum;
import com.qding.park.manager.facade.queue.service.enums.ArkOutOpSpecialEnum;
import com.qding.park.manager.facade.queue.service.enums.ArkOutOpTypeEnum;
import com.qding.park.manager.facade.queue.service.util.DicContrast;
import com.qding.park.manager.pojo.TbCar;
import com.qding.park.manager.pojo.TbFeetrade;
import com.qding.park.manager.pojo.TbInoutRecord;
import com.qding.park.manager.pojo.TbMonthRent;
import com.qding.park.manager.pojo.TbParkspace;
import com.qding.park.manager.pojo.ap.ApAppearanceRecord;
import com.qding.park.manager.pojo.ap.ApApproachRecord;
import com.qding.park.manager.pojo.ap.ApCarLockAlarm;
import com.qding.park.manager.pojo.ap.ApMonthlyFeeInfo;
import com.qding.park.manager.pojo.ap.ApReservationInReminder;
import com.qding.park.manager.pojo.ap.ApVisitingReminder;
import com.qding.park.manager.pojo.enums.CarOpSpecialEnum;
import com.qding.park.manager.pojo.enums.CarOpTypeEnum;
import com.qding.park.manager.pojo.enums.CardTypeEnum;
import com.qding.park.manager.pojo.enums.MonthRentStatuEnum;
import com.qding.park.manager.pojo.enums.OrderServiceType;
import com.qding.park.manager.pojo.enums.OrderSourceEnum;
import com.qding.park.manager.pojo.enums.RecordStatusEnum;
import com.qding.park.manager.pojo.noncore.NcExceptionParkOut;
import com.qding.park.manager.pojo.noncore.NcMonthRent;
import com.qding.park.manager.pojo.noncore.NcParkInoutInfo;
import com.qding.park.manager.service.queue.biz.InviteBiz;
import com.qding.park.manager.service.queue.biz.LockCarBiz;
import com.qding.park.manager.service.queue.biz.MonthBiz;
import com.qding.park.manager.service.queue.biz.ReserveBiz;
import com.qding.park.manager.service.queue.dao.MongodbDao;
import com.qding.park.manager.service.queue.mapper.TbCarMapper;
import com.qding.park.manager.service.queue.mapper.TbFeetradeMapper;
import com.qding.park.manager.service.queue.mapper.TbInoutRecordMapper;
import com.qding.park.manager.service.queue.mapper.TbMonthRentMapper;
import com.qding.park.manager.service.queue.mapper.TbParkspaceMapper;

/**
 * @ClassName: ReportFacadeImpl
 * @Description: TODO(上报业务实现)
 * @author Administrator
 * @date 2016年9月9日
 */
@Service("reportFacade")
public class ReportFacadeImpl implements ReportFacade {

    private static final Log _log = LogFactory.getLog(ReportFacadeImpl.class);

    @Autowired
    private TbParkspaceMapper tbParkspaceMapper;

    @Autowired
    private TbInoutRecordMapper tbInoutRecordMapper;

    @Autowired
    private CarCacheInter carCacheInter;

    @Autowired
    private TbCarMapper tbCarMapper;

    /**
     * @Fields mongodbDao : TODO(非核心数据源)
     */
    @Autowired
    private MongodbDao mongodbDao;

    @Autowired
    private UserCacheInter userCacheInter;

    @Autowired
    private ParkCacheInter parkCacheInter;

    @Autowired
    private IExceptionHandle exceptionHandle;

    @Autowired
    private TbMonthRentMapper tbMonthRentMapper;

    @Autowired
    private TbFeetradeMapper tbFeetradeMapper;

    @Autowired
    private LockCarBiz lockCarBiz;

    @Autowired
    private InviteBiz inviteBiz;

    @Autowired
    private ReserveBiz reserveBiz;

    @Autowired
    private MonthBiz monthBiz;

    @Autowired
    private MongoTemplate mongoTemplate;

    /*
     * (非 Javadoc) 来访提醒推送 业务
     * @param apVisitingReminder
     * @see com.qding.park.manager.facade.report.service.ReportFacade#
     * park_EventAlert_ParkingApply(com.qding.park.target.common.pojo. ApVisitingReminder)
     */
    @Override
    public void park_EventAlert_ParkingApply(ApVisitingReminder apVisitingReminder) throws BaseException {

    }

    /*
     * (非 Javadoc) 车辆进场上报业务
     * @param apApproachRecord
     * @see com.qding.park.manager.facade.report.service.ReportFacade#Park_ParkInSync
     * (com.qding.park.target.common.pojo.ApApproachRecord)
     */
    @Override
    public void Park_ParkInSync(ApApproachRecord apApproachRecord) {

        NcParkInoutInfo ncParkInoutInfo = new NcParkInoutInfo();
        // NcParkInInfo ncParkInInfo=new NcParkInInfo();
        String cardCode = apApproachRecord.getCardCode();
        int cardType = apApproachRecord.getCardType();
        int carType = apApproachRecord.getCarType();
        String carNum = apApproachRecord.getCarNum();
        String enInID = apApproachRecord.getEnInID();
        Date enterTime = apApproachRecord.getEnterTime();
        String parkCode = apApproachRecord.getParkId();
        String roomName = apApproachRecord.getRoomName();
        String supplyID = apApproachRecord.getSupplyID();
        String passID = apApproachRecord.getPassID();
        String parkInPic = apApproachRecord.getParkInPic();

        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, Integer.valueOf(supplyID));
        ncParkInoutInfo.setCardCode(cardCode);
        ncParkInoutInfo.setCarNum(carNum);
        ncParkInoutInfo.setCarType(carType);
        ncParkInoutInfo.setEnInID(enInID);
        ncParkInoutInfo.setCardType(cardType);
        ncParkInoutInfo.setEnterTime(enterTime);
        ncParkInoutInfo.setParkCode(parkCode);
        ncParkInoutInfo.setSupplyID(supplyID);
        ncParkInoutInfo.setRoomName(roomName);
        ncParkInoutInfo.setCreate_time(new Date());
        ncParkInoutInfo.setParkId(tbParkspace.getId());
        ncParkInoutInfo.setCreate_time(new Date());
        ncParkInoutInfo.setParkInPic(parkInPic);
        ncParkInoutInfo.setPassID(passID);
        List<TbCar> cars = tbCarMapper.queryCarByParkIdCarNum(tbParkspace.getId(), carNum);
        if (!ListUtil.isEmpty(cars)) {
            String name = "";
            String cardName = "";
            int userId = cars.get(0).getUserID();
            cardName = cars.get(0).getCardName();
            String tel = userCacheInter.userId2tel(String.valueOf(userId));
            name = userCacheInter.tel2name(tel);
            ncParkInoutInfo.setTel(tel);
            ncParkInoutInfo.setMemberName(name);
            ncParkInoutInfo.setCardName(cardName);
        }
        mongodbDao.saveIncheck(ncParkInoutInfo);

        TbInoutRecord record = new TbInoutRecord();
        record.setCar_type(ncParkInoutInfo.getCarType());
        record.setCarNum(ncParkInoutInfo.getCarNum());
        record.setCheckInTime(ncParkInoutInfo.getEnterTime());
        record.setInPointID(ncParkInoutInfo.getRoomName());
        record.setParkCode(ncParkInoutInfo.getParkCode());
        record.setSupplyID(Integer.valueOf(ncParkInoutInfo.getSupplyID()));
        record.setInPointID(ncParkInoutInfo.getRoomName());
        record.setEnInID(ncParkInoutInfo.getEnInID());
        record.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
        // 入场
        inCheck(record);

    }

    private void inCheck(TbInoutRecord record) {
        // TODO Auto-generated method stub
        int supplyID = record.getSupplyID();
        String parkCode = record.getParkCode();
        String carNum = record.getCarNum();

        List<TbCar> userCars = tbCarMapper.queryCarOfParkByCarNum(carNum);
        if (ListUtil.isEmpty(userCars)) {
            // 不是必要的通行记录
            _log.info("不是必要的通行记录");
            return;
        } else {
            TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, supplyID);

            int parkId = tbParkspace.getId();
            String parkName = tbParkspace.getName();
            record.setParkID(parkId);
            record.setParkName(parkName);
            for (TbCar car : userCars) {
                int userId = car.getUserID();
                record.setUserID(userId);
                tbInoutRecordMapper.save(record);
            }
            return;
        }

    }

    /*
     * (非 Javadoc) 车辆出场上报业务
     * @param apAppearanceRecord
     * @see com.qding.park.manager.facade.report.service.ReportFacade#
     * Park_ParkOutSync(com.qding.park.target.common.pojo.ApAppearanceRecord)
     */
    @Override
    public void Park_ParkOutSync(ApAppearanceRecord apAppearanceRecord) throws ServiceCoreException {

        if (!StringUtils.isBlank(apAppearanceRecord.getPayOrderID())) {
            // 不处理线上缴费上报单
            return;
        }

        // NcParkOutInfo ncParkOutInfo=new NcParkOutInfo();
        NcParkInoutInfo ncParkInoutInfo = new NcParkInoutInfo();
        String cardCode = apAppearanceRecord.getCardCode();
        int cardType = apAppearanceRecord.getCardType();
        String carNum = apAppearanceRecord.getCarNum();
        int carType = apAppearanceRecord.getCarType();
        String enInID = apAppearanceRecord.getEnInID();
        String enOutID = apAppearanceRecord.getEnOutID();
        Date enterTime = apAppearanceRecord.getEnterTime();
        int feeMoney = apAppearanceRecord.getFeeMoney();
        int opSpecial = apAppearanceRecord.getOpSpecial();
        int opType = apAppearanceRecord.getOpType();
        String outRoomName = apAppearanceRecord.getOutRoomName();
        Date outTime = apAppearanceRecord.getOutTime();
        String parkCode = apAppearanceRecord.getParkId();
        String roomName = apAppearanceRecord.getRoomName();
        String parkInPic = apAppearanceRecord.getParkInPic();
        String parkOutPic = apAppearanceRecord.getParkOutPic();
        int shouldFeeMoney = apAppearanceRecord.getShouldFeeMoney();
        String operator = apAppearanceRecord.getOpName();
        String supplyID = apAppearanceRecord.getSupplyID();
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, Integer.valueOf(supplyID));

        List<TbCar> cars = tbCarMapper.queryCarByParkIdCarNum(tbParkspace.getId(), carNum);
        if (!ListUtil.isEmpty(cars)) {
            String name = "";
            String cardName = "";
            int userId = cars.get(0).getUserID();
            cardName = cars.get(0).getCardName();
            String tel = userCacheInter.userId2tel(String.valueOf(userId));
            name = userCacheInter.tel2name(tel);
            ncParkInoutInfo.setTel(tel);
            ncParkInoutInfo.setMemberName(name);
            ncParkInoutInfo.setCardName(cardName);
        }
        ncParkInoutInfo.setCardCode(cardCode);
        ncParkInoutInfo.setCardType(cardType);
        ncParkInoutInfo.setCarNum(carNum);
        ncParkInoutInfo.setCarType(carType);
        ncParkInoutInfo.setEnInID(enInID);
        ncParkInoutInfo.setEnOutID(enOutID);
        ncParkInoutInfo.setCreate_time(new Date());
        ncParkInoutInfo.setEnterTime(enterTime);
        ncParkInoutInfo.setFeeMoney(shouldFeeMoney);
        ncParkInoutInfo.setOpSpecial(opSpecial);
        ncParkInoutInfo.setOpType(opType);
        ncParkInoutInfo.setOutRoomName(outRoomName);
        ncParkInoutInfo.setOutTime(outTime);
        ncParkInoutInfo.setParkCode(parkCode);
        ncParkInoutInfo.setRoomName(roomName);
        ncParkInoutInfo.setShouldFeeMoney(shouldFeeMoney);
        ncParkInoutInfo.setSupplyID(supplyID);
        ncParkInoutInfo.setParkId(tbParkspace.getId());
        ncParkInoutInfo.setPayOrderID(apAppearanceRecord.getPayOrderID());
        ncParkInoutInfo.setParkInPic(parkInPic);
        ncParkInoutInfo.setParkOutPic(parkOutPic);
        ncParkInoutInfo.setOperator(operator);
        mongodbDao.saveOutcheck(ncParkInoutInfo);

        // 更新mysql 数据库中的进出场记录
        TbInoutRecord tbInoutRecord = new TbInoutRecord();
        tbInoutRecord.setCar_type(ncParkInoutInfo.getCarType());
        tbInoutRecord.setCard_type(ncParkInoutInfo.getCardType());
        tbInoutRecord.setCarNum(ncParkInoutInfo.getCarNum());
        tbInoutRecord.setCheckInTime(ncParkInoutInfo.getEnterTime());
        tbInoutRecord.setCheckOutTime(ncParkInoutInfo.getOutTime());
        tbInoutRecord.setEnInID(ncParkInoutInfo.getEnInID());
        tbInoutRecord.setEnOutID(enOutID);
        tbInoutRecord.setEnOutID(enOutID);
        tbInoutRecord.setFeetotal(feeMoney);
        tbInoutRecord.setInPointID(ncParkInoutInfo.getRoomName());
        tbInoutRecord.setOpSpecial(opSpecial);
        tbInoutRecord.setOptype(ncParkInoutInfo.getOpType());
        tbInoutRecord.setOutPointID(ncParkInoutInfo.getOutRoomName());
        tbInoutRecord.setParkCode(parkCode);
        tbInoutRecord.setSupplyID(Integer.valueOf(ncParkInoutInfo.getSupplyID()));

        outCheck(tbInoutRecord);// 更新mysql

    }

    private void outCheck(TbInoutRecord record) throws ServiceCoreException {
        // TODO Auto-generated method stub
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(record.getParkCode(),
                record.getSupplyID());

        if (tbParkspace == null) {
            // 车辆没有停在此车场
            throw new ServiceCoreException(ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getId(), null,
                    ExceptionMessage.createExceptionMessage(
                            ServiceCoreException.TypeEnum.EXCEPTION_NOFIND_PARK.getDesc(), "没有找到对应的车场信息",
                            "parkCode=" + record.getParkCode() + " ,supplyID=" + record.getSupplyID()));
        }
        int parkId = tbParkspace.getId();
        String parkName = tbParkspace.getName();

        record.setParkID(parkId);
        record.setParkName(parkName);

        // 更新对应的出场记录
        tbInoutRecordMapper.outCheck(record);

    }

    @Override
    public void Park_MonthlyFeeSync(ApMonthlyFeeInfo apMonthlyFeeInfo) throws BaseException {
        // 线下缴费模式
        int addFeeModel = apMonthlyFeeInfo.getAddFeeMode();

        // 卡号
        String cardCode = apMonthlyFeeInfo.getCardCode();
        // 车牌号
        String carNum = apMonthlyFeeInfo.getCarNum();
        // 用户名
        String memberName = apMonthlyFeeInfo.getMemberName();
        // 月租结束时间
        Date monthlyEndTime = apMonthlyFeeInfo.getMonthlyEndTime();
        // 月租开始时间
        Date monthlyStartTime = apMonthlyFeeInfo.getMonthlyStartTime();
        // 车场缴费单编号
        String payId = apMonthlyFeeInfo.getPayID();
        // 缴费金额
        int paymentAmount = apMonthlyFeeInfo.getPaymentAmount();
        // 线上缴费单号
        String payOrderId = apMonthlyFeeInfo.getPayOrderID();
        // 续费时间
        Date rechargeDate = apMonthlyFeeInfo.getRechargeDate();
        // 应缴费金额
        int shouldPaymentAmount = apMonthlyFeeInfo.getShouldPaymentAmount();

        String parkCode = apMonthlyFeeInfo.getParkCode();
        String supplyID = apMonthlyFeeInfo.getSupplyID();
        TbParkspace tbParkspace = parkCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyID));
        int parkId = tbParkspace.getId();
        String parkName = tbParkspace.getName();
        int orderSource = OrderSourceEnum.OUTLINE.getType();

        // List<String> appOrderlist = new ArrayList<String>();
        String monthid = "";
        List<TbCar> tbCars = tbCarMapper.queryCarByParkIdCarNum(parkId, carNum);
        int userId = 0;
        String bjuserid = "";
        int monthlyBuyFee = 0;
        String tel = "";
        try {
            // 回写到mysql中去
            if (!ListUtil.isEmpty(tbCars)) {
                userId = tbCars.get(0).getUserID();
                bjuserid = userCacheInter.userId2bjuserId(String.valueOf(userId));
                tel = userCacheInter.userId2tel(String.valueOf(userId));
                monthlyBuyFee = tbCars.get(0).getMonthFee();
                String tradeId = OrderNumUtil.getOrderNum(userId, OrderServiceType.FEETRADE.getTag());
                monthid = OrderNumUtil.getOrderNum(userId, OrderServiceType.OUTLINE_MONTHRENT.getTag());
                // appOrderlist.add(monthid);
                TbMonthRent tbMonthRent = new TbMonthRent();
                tbMonthRent.setParkID(parkId);
                tbMonthRent.setUserID(userId);
                tbMonthRent.setCarNum(carNum);
                tbMonthRent.setId(monthid);
                tbMonthRent.setParkName(tbParkspace.getName());
                tbMonthRent.setStatus(MonthRentStatuEnum.PAYED_PARKED.getStatus());
                tbMonthRent.setOldExpiredate(monthlyStartTime);
                tbMonthRent.setExpiredate(monthlyEndTime);
                int monthcount = DateUtils.getMonthDiff(monthlyStartTime, monthlyEndTime);
                tbMonthRent.setBuyMonths(monthcount);
                tbMonthRent.setTradeID(tradeId);
                tbMonthRent.setCost(shouldPaymentAmount);
                tbMonthRent.setRealPayCost(paymentAmount);
                tbMonthRent.setParkOrderId(payId);
                tbMonthRent.setOrderSource(orderSource);
                tbMonthRent.setRemark("线下月租续费单");
                tbMonthRent.setCreate_date(new Date());
                tbMonthRent.setUpdate_date(new Date());
                tbMonthRentMapper.save(tbMonthRent);

                TbFeetrade tbFeetrade = new TbFeetrade();
                tbFeetrade.setId(tradeId);
                tbFeetrade.setUserID(tbMonthRent.getUserID());
                tbFeetrade.setParkID(tbMonthRent.getParkID());
                tbFeetrade.setFeeType(TbFeetrade.FeeTypeEnum.CAPITALFLOW_MONTH_PAY.getType());
                tbFeetrade.setFeeCost(paymentAmount);// 实付金额
                tbFeetrade.setBusinessID(tbMonthRent.getId());
                tbFeetrade.setChannel(TbFeetrade.ChannelEnum.CAPITALFLOW_LINE.getType());
                tbFeetrade.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
                tbFeetradeMapper.save(tbFeetrade);
            }
            // 由于是线上的月租单，随机取一个用户id
            // for (TbCar car : tbCars) {
            // int userId = car.getUserID();

            // }

        } catch (Exception e) {
            _log.error(apMonthlyFeeInfo + " 入mysql异常");
            exceptionHandle.handle(e);
        }

        // 报表统计
        NcMonthRent ncMonthRent = new NcMonthRent();
        // ncMonthRent.setAppOrderList(appOrderlist);
        ncMonthRent.setAddFeeMode(addFeeModel);
        ncMonthRent.setBjOrderId(null);
        ncMonthRent.setCardCode(cardCode);
        ncMonthRent.setCarNum(carNum);
        ncMonthRent.setCreate_date(new Date());
        ncMonthRent.setMonthlyEndTime(monthlyEndTime);
        ncMonthRent.setMonthlyStartTime(monthlyStartTime);
        ncMonthRent.setOrderSource(orderSource);
        ncMonthRent.setParkID(parkId);
        ncMonthRent.setParkName(parkName);
        ncMonthRent.setParkPayId(payId);
        ncMonthRent.setSupplyID(supplyID);
        ncMonthRent.setUserId(userId);
        // 线下月租订单，我们要生成一个订单号，以便查询
        // String payOrderID = OrderNumUtil.getOrderNum(supplyID + payId, OrderServiceType.OUTLINE_MONTHRENT.getTag());
        String payOrderID = (monthid == null
                ? OrderNumUtil.getOrderNum(supplyID + payId, OrderServiceType.OUTLINE_MONTHRENT.getTag()) : monthid);
        ncMonthRent.setPayOrderID(payOrderID);
        ncMonthRent.setPay_channel(TbFeetrade.ChannelEnum.CAPITALFLOW_LINE.getType());
        ncMonthRent.setPaymentAmount(paymentAmount);
        ncMonthRent.setRechargeDate(rechargeDate);
        ncMonthRent.setShouldPaymentAmount(shouldPaymentAmount);
        ncMonthRent.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
        ncMonthRent.setUpdate_date(new Date());
        ncMonthRent.setUserName(memberName);
        ncMonthRent.setBjuserid(bjuserid);
        ncMonthRent.setMonthlyBuyFee(monthlyBuyFee);
        ncMonthRent.setTel(tel);
        ncMonthRent.setBuyMonths(
                DateUtils.getMonthDiff(ncMonthRent.getMonthlyStartTime(), ncMonthRent.getMonthlyEndTime()));
        // DateUtils.compareDate(date, otherDate, withUnit);
        try {
            mongodbDao.saveNcMonthRent(ncMonthRent);

            tbCarMapper.parkMonthlyFeeReport(parkId, carNum, monthlyEndTime);
        } catch (Exception e) {
            _log.error(ncMonthRent + " 入mongodb异常");
            exceptionHandle.handle(e);
        }
    }

    @Override
    public void alarmCockCar(ApCarLockAlarm apCarLockAlarm) throws BaseException {
        lockCarBiz.alarmCockCar(apCarLockAlarm);
    }

    @Override
    public void inviteVisited(ApVisitingReminder apVisitingReminder) throws BaseException {
        inviteBiz.inviteVisited(apVisitingReminder);
    }

    @Override
    public void carInCheck(ApReservationInReminder apReservationInReminder) throws BaseException {
        reserveBiz.carInCheck(apReservationInReminder);
    }

    /*********** v1.0 方舟上报 ******************************/
    /* (非 Javadoc)
     * Description:方舟 异常出场记录上报
     * @see com.qding.park.manager.facade.queue.service.ReportFacade#exceptionParkOutSync(com.qding.park.manager.facade.queue.service.entity.ExceptionParkOutSyncDTO)
     */
    @Override
    public void exceptionParkOutSync(ExceptionParkOutSyncDTO dto) throws BaseException {
        String supplyId = dto.getSupplyId();
        String parkCode = dto.getParkCode();
        String enOutId = dto.getEnOutId();
        // String exceptionType=dto.getExceptionType();
        Date outTime = dto.getOutTime();
        String outDevice = dto.getOutDevice();
        String parkOutPic = dto.getParkOutPic();
        TbParkspace parkSpace = parkCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyId));

        NcExceptionParkOut out = new NcExceptionParkOut();
        out.setSupplyId(supplyId);
        out.setParkCode(parkCode);
        out.setEnOutId(enOutId);
        out.setOutRoom(outDevice);
        out.setParkOutPic(parkOutPic);
        out.setOutTime(outTime);
        out.setParkId(parkSpace.getId());
        mongoTemplate.insert(out, MongodbConstant.TABLENAME_EXCEPTION_CHECKOUT);
    }

    /* (非 Javadoc)
     * Description:方舟事件上报
     * @see com.qding.park.manager.facade.queue.service.ReportFacade#parkEventSync(com.qding.park.manager.facade.queue.service.entity.ParkEventSyncDTO)
     */
    @Override
    public void parkEventSync(ParkEventSyncDTO dto) throws BaseException {

        ArkEventTypeEnum eventType = ArkEventTypeEnum.valueOf(dto.getEventType());

        // 停车场id
        String parkCode = dto.getParkCode();
        // 车场软件id
        String supplyId = dto.getSupplyId();
        if (eventType == ArkEventTypeEnum.alarm_lockcar) {
            // 锁车出场报警
            Date alarmTime = dto.getAlarmTime();
            // 车牌
            String carNum = dto.getCarNum();
            // 出场岗停
            String ourRoom = dto.getOutDevice();

            lockCarBiz.alarmCockCarArk(supplyId, parkCode, ourRoom, carNum, alarmTime);

        } else if (eventType == ArkEventTypeEnum.notice_visitor) {
            // 来访通知
            String carNum = dto.getCarNum();
            Date inDateTime = dto.getEnterTime();
            String inRoom = dto.getEnInDevice();
            inviteBiz.inviteVisitedArk(supplyId, parkCode, carNum, inDateTime, inRoom);
        }
    }

    /* (非 Javadoc)
     * Description:方舟入场上报
     * @see com.qding.park.manager.facade.queue.service.ReportFacade#parkInSync(com.qding.park.manager.facade.queue.service.entity.ParkInSyncDTO)
     */
    @Override
    public void parkInSync(ParkInSyncDTO dto) throws BaseException {
        NcParkInoutInfo ncParkInoutInfo = new NcParkInoutInfo();
        // NcParkInInfo ncParkInInfo=new NcParkInInfo();
        String cardCode = "";
        ArkCarTypeEnum arkCarTypeEnum = ArkCarTypeEnum.valueOf(dto.getCardType());
        CardTypeEnum cardTypeEnum = DicContrast.ark2qdingCardType(arkCarTypeEnum);
        int cardType = cardTypeEnum.getType();
        String carNum = dto.getCarNum();
        String enInID = dto.getEnInId();
        Date enterTime = dto.getEnterTime();
        String parkCode = dto.getParkCode();
        String roomName = dto.getEnInDevice();
        String supplyID = dto.getSupplyId();
        String passID = dto.getEnInDevice();
        String parkInPic = dto.getParkInPic();

        TbParkspace parkspace = parkCacheInter.queryParkSpace(parkCode, Integer.valueOf(supplyID));
        // TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, Integer.valueOf(supplyID));
        ncParkInoutInfo.setCardCode(cardCode);
        ncParkInoutInfo.setCarNum(carNum);
        // ncParkInoutInfo.setCarType(carType);
        ncParkInoutInfo.setEnInID(enInID);
        ncParkInoutInfo.setCardType(cardType);
        ncParkInoutInfo.setEnterTime(enterTime);
        ncParkInoutInfo.setParkCode(parkCode);
        ncParkInoutInfo.setSupplyID(supplyID);
        ncParkInoutInfo.setRoomName(roomName);
        ncParkInoutInfo.setCreate_time(new Date());
        ncParkInoutInfo.setParkId(parkspace.getId());
        ncParkInoutInfo.setCreate_time(new Date());
        ncParkInoutInfo.setParkInPic(parkInPic);
        ncParkInoutInfo.setPassID(passID);
        List<TbCar> cars = tbCarMapper.queryCarByParkIdCarNum(parkspace.getId(), carNum);
        if (!ListUtil.isEmpty(cars)) {
            String name = "";
            String cardName = "";
            int userId = cars.get(0).getUserID();
            cardName = cars.get(0).getCardName();
            String tel = userCacheInter.userId2tel(String.valueOf(userId));
            name = userCacheInter.tel2name(tel);
            ncParkInoutInfo.setTel(tel);
            ncParkInoutInfo.setMemberName(name);
            ncParkInoutInfo.setCardName(cardName);
        }
        mongodbDao.saveIncheck(ncParkInoutInfo);

        TbInoutRecord record = new TbInoutRecord();
        record.setCar_type(ncParkInoutInfo.getCarType());
        record.setCarNum(ncParkInoutInfo.getCarNum());
        record.setCheckInTime(ncParkInoutInfo.getEnterTime());
        record.setInPointID(ncParkInoutInfo.getRoomName());
        record.setParkCode(ncParkInoutInfo.getParkCode());
        record.setSupplyID(Integer.valueOf(ncParkInoutInfo.getSupplyID()));
        record.setInPointID(ncParkInoutInfo.getRoomName());
        record.setEnInID(ncParkInoutInfo.getEnInID());
        record.setStatus(RecordStatusEnum.EFFICIENCY.getStatus());
        // 入场
        inCheck(record);
    }

    /*
     * (非 Javadoc) Description: 方舟出入场缴费记录上报
     * @see
     * com.qding.park.manager.facade.queue.service.ReportFacade#parkOutRenewalSync(com.qding.park.manager.facade.queue.
     * service.entity.ParkOutRenewalSyncDTO)
     */
    @Override
    public void parkOutRenewalSync(ParkOutRenewalSyncDTO dto) throws BaseException {
        String carNum = dto.getCarNum();
        String enInId = dto.getEnInId();
        String feeMoney = dto.getFeeMoney();
        String orderId = dto.getOrderId();
        String parkCode = dto.getParkCode();
        String payMode = dto.getPayMode();
        Date payTime = dto.getPayTime();
        String shouldFeeMoney = dto.getShouldFeeMoney();
        String supplyId = dto.getSupplyId();
        
        Criteria criteria = new Criteria();
        criteria.and("supplyID").is(supplyId);
        criteria.and("parkCode").is(parkCode);
        criteria.and("enInID").is(enInId);
        Query query = new Query();
        query.addCriteria(criteria);
        _log.info(criteria.getCriteriaObject());
        Update update = new Update();
        update.inc("feeMoney", Integer.valueOf(feeMoney));//在feeMoney基础上加上
        update.inc("shouldFeeMoney", Integer.valueOf(shouldFeeMoney));//在shouldFeeMoney基础上加上
        mongoTemplate.updateFirst(query, update, MongodbConstant.TABLENAME_CHECKOUT);
    }

    /* (非 Javadoc)
     * Description:方舟出场上报
     * @see com.qding.park.manager.facade.queue.service.ReportFacade#parkOutSync(com.qding.park.manager.facade.queue.service.entity.ParkOutSyncDTO)
     */
    @Override
    public void parkOutSync(ParkOutSyncDTO dto) throws BaseException {

        NcParkInoutInfo ncParkInoutInfo = new NcParkInoutInfo();
        ArkCarTypeEnum arkCarTypeEnum = ArkCarTypeEnum.valueOf(dto.getCardType());
        CardTypeEnum cardTypeEnum = DicContrast.ark2qdingCardType(arkCarTypeEnum);
        int cardType = cardTypeEnum.getType();
        String carNum = dto.getCarNum();
        String enInID = dto.getEnInId();
        String enOutID = dto.getEnOutId();
        Date enterTime = dto.getEnterTime();
        int feeMoney = Integer.valueOf(dto.getFeeMoney());

        ArkOutOpSpecialEnum arkOutOpSpecialEnum = ArkOutOpSpecialEnum.valueOf(dto.getOutOpSpecial());
        CarOpSpecialEnum carOpSpecialEnum = DicContrast.ark2qdingOpSpecial(arkOutOpSpecialEnum);
        int opSpecial = carOpSpecialEnum.getType();
        ArkOutOpTypeEnum arkOutOpTypeEnum = ArkOutOpTypeEnum.valueOf(dto.getOutOpType());
        CarOpTypeEnum carOpTypeEnum = DicContrast.ark2qdingOpType(arkOutOpTypeEnum);
        int opType = carOpTypeEnum.getType();
        String outRoomName = dto.getOutDevice();
        Date outTime = dto.getOutTime();
        String parkCode = dto.getParkCode();
        String roomName = dto.getEnInDevice();
        String parkInPic = dto.getParkInPic();
        String parkOutPic = dto.getParkOutPic();
        int shouldFeeMoney = Integer.valueOf(dto.getShouldFeeMoney());
        String operator = dto.getOpName();
        String supplyID = dto.getSupplyId();
        TbParkspace tbParkspace = tbParkspaceMapper.queryParkInfoByCodeSupplier(parkCode, Integer.valueOf(supplyID));

        int parkId = tbParkspace.getId();
        List<TbCar> cars = carCacheInter.carNum2Info(carNum);
        if (!ListUtil.isEmpty(cars)) {
            ncParkInoutInfo.setTel(monthBiz.cars2tel(cars));
            ncParkInoutInfo.setMemberName(monthBiz.cars2memberName(cars));
            ncParkInoutInfo.setCardName(monthBiz.cars2cardName(cars));
        }
        // ncParkInoutInfo.setCardCode(cardCode);
        ncParkInoutInfo.setCardType(cardType);
        ncParkInoutInfo.setCarNum(carNum);
        // ncParkInoutInfo.setCarType(carType);
        ncParkInoutInfo.setEnInID(enInID);
        ncParkInoutInfo.setEnOutID(enOutID);
        ncParkInoutInfo.setCreate_time(new Date());
        ncParkInoutInfo.setEnterTime(enterTime);
        ncParkInoutInfo.setFeeMoney(shouldFeeMoney);
        ncParkInoutInfo.setOpSpecial(opSpecial);
        ncParkInoutInfo.setOpType(opType);
        ncParkInoutInfo.setOutRoomName(outRoomName);
        ncParkInoutInfo.setOutTime(outTime);
        ncParkInoutInfo.setParkCode(parkCode);
        ncParkInoutInfo.setRoomName(roomName);
        ncParkInoutInfo.setShouldFeeMoney(shouldFeeMoney);
        ncParkInoutInfo.setSupplyID(supplyID);
        ncParkInoutInfo.setParkId(parkId);
        // ncParkInoutInfo.setPayOrderID(apAppearanceRecord.getPayOrderID());
        ncParkInoutInfo.setParkInPic(parkInPic);
        ncParkInoutInfo.setParkOutPic(parkOutPic);
        ncParkInoutInfo.setOperator(operator);
        // mongodbDao.saveOutcheck(ncParkInoutInfo);

        Criteria criteria = new Criteria();
        criteria.and("parkId").is(parkId);
        criteria.and("enInID").is(enInID);
        Query query = new Query();
        query.addCriteria(criteria);
        _log.info(criteria.getCriteriaObject());
        Update update = new Update();
        update.set("enOutID", enOutID);
        update.set("orderSource", ncParkInoutInfo.getOrderSource());
        update.set("outTime", ncParkInoutInfo.getOutTime());
        update.set("outRoomName", ncParkInoutInfo.getOutRoomName());
        update.set("opType", ncParkInoutInfo.getOpType());
        update.set("opSpecial", ncParkInoutInfo.getOpSpecial());
        update.set("cardType", ncParkInoutInfo.getCardType());
        update.set("shouldFeeMoney", shouldFeeMoney == 0 ? ncParkInoutInfo.getShouldFeeMoney() : shouldFeeMoney);
        update.set("feeMoney", feeMoney == 0 ? ncParkInoutInfo.getFeeMoney() : feeMoney);
        update.set("update_time", ncParkInoutInfo.getUpdate_time());
        update.set("carType", ncParkInoutInfo.getCarType());
        update.set("update_time", new Date());
        update.set("operator", ncParkInoutInfo.getOperator());
        update.set("parkInPic", ncParkInoutInfo.getParkInPic());
        update.set("parkOutPic", ncParkInoutInfo.getParkOutPic());

        // 更新mysql 数据库中的进出场记录
        TbInoutRecord tbInoutRecord = new TbInoutRecord();
        tbInoutRecord.setCar_type(ncParkInoutInfo.getCarType());
        tbInoutRecord.setCard_type(ncParkInoutInfo.getCardType());
        tbInoutRecord.setCarNum(ncParkInoutInfo.getCarNum());
        tbInoutRecord.setCheckInTime(ncParkInoutInfo.getEnterTime());
        tbInoutRecord.setCheckOutTime(ncParkInoutInfo.getOutTime());
        tbInoutRecord.setEnInID(ncParkInoutInfo.getEnInID());
        tbInoutRecord.setEnOutID(enOutID);
        tbInoutRecord.setEnOutID(enOutID);
        tbInoutRecord.setFeetotal(feeMoney);
        tbInoutRecord.setInPointID(ncParkInoutInfo.getRoomName());
        tbInoutRecord.setOpSpecial(opSpecial);
        tbInoutRecord.setOptype(ncParkInoutInfo.getOpType());
        tbInoutRecord.setOutPointID(ncParkInoutInfo.getOutRoomName());
        tbInoutRecord.setParkCode(parkCode);
        tbInoutRecord.setSupplyID(Integer.valueOf(ncParkInoutInfo.getSupplyID()));

        outCheck(tbInoutRecord);// 更新mysql

        /* 更新mongodb数据库 */
        mongoTemplate.updateMulti(query, update, NcParkInoutInfo.class, MongodbConstant.TABLENAME_CHECKINOUT);

        mongoTemplate.insert(ncParkInoutInfo, MongodbConstant.TABLENAME_CHECKOUT);
    }

    /* (非 Javadoc)
     * Description:方舟月卡充值上报
     * @see com.qding.park.manager.facade.queue.service.ReportFacade#renewalSync(com.qding.park.manager.facade.queue.service.entity.RenewalSyncDTO)
     */
    @Override
    public void renewalSync(RenewalSyncDTO dto) throws BaseException {
        monthBiz.monthFeeSync(dto);
    }

}
